package com.lovo.cloud.service.impl;

import com.lovo.cloud.model.dto.OrderRateDTO;
import com.lovo.cloud.redis.RedisDAO;
import com.lovo.cloud.service.OrderRateService;
import com.lovo.cloud.util.SerializeUtil;
import org.redisson.api.RedissonClient;
import org.springframework.stereotype.Service;
import redis.clients.jedis.Jedis;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @author luo
 * @description
 * @date 2020/12/9 17:39
 */
@Service
public class OrderRateServiceImpl implements OrderRateService {

    @Resource
    private RedissonClient redisson;

    @Override
    public void addRate(OrderRateDTO orderRateDTO) {
        Jedis jedis = RedisDAO.getRedis();
        byte[] bytes = jedis.get(orderRateDTO.getOrderNum().getBytes());
        if (bytes != null) {
            List<String> list = (List<String>) SerializeUtil.unserizlize(bytes);
            System.err.println(orderRateDTO.getOrderNum()+"============="+list);
            list.add(orderRateDTO.getRate());
            jedis.setex(orderRateDTO.getOrderNum().getBytes(),1000*60*60,SerializeUtil.serialize(list));
        }else {
            List<String> list = new ArrayList<>();
            list.add(orderRateDTO.getRate());
            jedis.setex(orderRateDTO.getOrderNum().getBytes(),1000*60*60,SerializeUtil.serialize(list));
        }

        System.out.println(orderRateDTO.getOrderNum()+"------------------"+SerializeUtil.unserizlize(jedis.get(orderRateDTO.getOrderNum().getBytes())));

    }
}
